package cn.hrlly.tao2shou.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.presenter.LoginPresenter;
import cn.hrlly.tao2shou.presenter.LoginPresenterImpl;
import cn.hrlly.tao2shou.utils.GpsUtil;
import cn.hrlly.tao2shou.view.view_interface.LoginView;
import cn.hrlly.tao2shou.view.base.BaseActivity;

public class LoginActivity extends Activity implements LoginView {

    @Bind(R.id.etPhoneNumber)
    EditText etPhoneNumber;
    @Bind(R.id.etPassword)
    EditText etPassword;
    @Bind(R.id.btnRegister)
    Button btnRegister;
    @Bind(R.id.btnLogin)
    Button btnLogin;
    @Bind(R.id.tvFindPassword)
    TextView tvFindPassword;
    private GpsUtil gpsUtil;
    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //将控件绑定
        ButterKnife.bind(this);
        //初始化定位工具类,并开始定位
        gpsUtil = new GpsUtil(getApplicationContext());
        gpsUtil.startLocation();
        //初始化loginPresenter
        loginPresenter = new LoginPresenterImpl(this, this);
    }

    //activity取消时停止定位,关闭数据库
    @Override
    protected void onDestroy() {
        super.onDestroy();
        gpsUtil.stopLocation();
        loginPresenter.closeDataBase();
    }

    //btnLogin点击事件
    @OnClick(R.id.btnLogin)
    public void login() {
        if (!etPhoneNumber.getText().toString().equals("") && !etPassword.getText().toString().equals("")) {
            if(etPhoneNumber.getText().toString().length()==11)
                loginPresenter.login();
            else
                showMessage(R.string.phone_number_is_error);
        } else {
            showMessage(R.string.fill_incomplete);
        }
    }

    //btnRegister点击事件，跳转至RegisterAcitivity
    @OnClick(R.id.btnRegister)
    public void register() {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }

    //tvPassword点击事件，跳转至FindPasswordActivity
    @OnClick(R.id.tvFindPassword)
    public void findPassword(){
        Intent intent=new Intent(LoginActivity.this,FindPasswordActivity.class);
        startActivity(intent);
    }

    //以下是对activity的一些操作
    @Override
    public String getPhoneNumber() {
        return etPhoneNumber.getText().toString();
    }

    @Override
    public String getPassword() {
        return etPassword.getText().toString();
    }

    @Override
    public double getLongitude() {
        return gpsUtil.returnLongitude();
    }

    @Override
    public double getLantitude() {
        return gpsUtil.returnLantitude();
    }


    @Override
    public void showMessage(String errorMessage) {
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int errorMessage) {
        Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void startActivity() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }
}
